package com.zhuiyun.project.api.lottery.mapper;

import com.zhuiyun.project.api.lottery.entity.Args;
import com.zhuiyun.project.api.lottery.entity.Lottery;
import com.zhuiyun.project.api.lottery.model.LotteryModel;
import com.zhuiyun.project.common.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * @ClassName LotteryMapper
 * @Description TODO
 * @Author 17179
 * @Date 2024/7/16 15:51
 **/
public interface LotteryMapper extends BaseMapper<Lottery> {

    /**
     * 查询数据总条数
     * @return
     */
    @Select("select count(1) from lottery where lottery_id = #{type}")
    int selectLotteryCount(String type);

    /**
     * 批量新增 去重重复数据
     * @param list
     * @return
     */
    @Insert({
            "<script>",
            "INSERT IGNORE INTO lottery (lottery_id, lottery_res,lottery_no,lottery_date,lottery_exdate) VALUES ",
            "<foreach collection='list' item='item' index='index' separator=','>",
            "(#{item.lottery_id}, #{item.lottery_res}, #{item.lottery_no}, #{item.lottery_date}, #{item.lottery_exdate})",
            "</foreach>",
            "</script>"
    })
    int insertIgnoreBatch(List<Lottery> list);

    /**
     * 获取 102条最新的
     * @return
     */
    @Select("<script> select * from (select " +
            " lottery_id,lottery_no,lottery_date," +
            " CASE  " +
            "        WHEN #{args.type} = 'plwa3' THEN CONCAT(SUBSTR(lottery_res, 5),',' ,SUBSTR(lottery_res, 1, 3)) " +
            "        ELSE lottery_res   " +
            "           END AS lottery_res " +
            " from lottery l where 1=1 and " +
            " CASE " +
            "   WHEN #{args.type} = 'plwa3' THEN lottery_id = 'plw' " +
            "   ELSE  lottery_id = #{args.type}" +
            " END" +
            " <when test='args.first!=0 and args.second!=0' > and l.lottery_no between #{args.first} and  #{args.second}</when> " +
            "order by lottery_no desc " +
            "<when test='args.count!=0 ' >  limit 0,#{args.count}</when> )" +
            " l order by l.lottery_no asc </script>")
    List<LotteryModel> select105data(@Param("args") Args args);

    @Select("select lottery_no as text,lottery_no as value from lottery where lottery_id=#{type} order by lottery_no asc")
    List<Map> getLotteryNo(String type);

    @Select("select lottery_no  from lottery where lottery_id=#{type} order by lottery_no desc limit 1")
    String getLastLotteryNo(String type);

    @Select("select lottery_res  from lottery where lottery_id=#{type} order by lottery_no desc limit 1")
    String getLastLotteryRes(String type);

    @Select("SELECT COUNT(1) AS count" +
            "FROM lottery " +
            "WHERE DATE(lottery_date) = CURDATE();")
    int getNewLottery();
}
